package com.question.controller.admin;

import com.github.pagehelper.PageInfo;
import com.question.constant.RedisKeyConstant;
import com.question.pojo.dto.QuestionDTO;
import com.question.pojo.result.PageResult;
import com.question.pojo.result.Result;
import com.question.pojo.vo.QuestionVO;
import com.question.service.CategoryService;
import com.question.service.MajorService;
import com.question.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

@RestController("adminQuestionController")
@RequestMapping("/admin/manage/questions")
@Slf4j
@Api(tags = "题目管理相关接口")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

//    /**
//     * 获取题目列表（分页） - 使用 PageHelper 实现
//     * @param pageNum
//     * @param pageSize
//     * @param questionType
//     * @param keyword
//     * @param categoryId
//     * @return
//     */
//    @GetMapping("")
//    @ApiOperation("获取题目列表（分页）")
//    public Result<PageInfo<QuestionVO>> listQuestions(
//            @RequestParam(defaultValue = "1") Integer pageNum,
//            @RequestParam(defaultValue = "10") Integer pageSize,
//            @RequestParam(required = false) String questionType,
//            @RequestParam(required = false) String keyword,
//            @RequestParam(required = false) Integer categoryId,
//            @RequestParam(required = false) Integer majorId) {
//
//        // 添加日志验证参数
//        log.info("接收分页参数 - pageNum: {}, pageSize: {}, majorId: {}", pageNum, pageSize, majorId);
//
//        PageInfo<QuestionVO> result = questionService.listQuestions(
//                pageNum, pageSize, questionType, keyword, categoryId,majorId
//        );
//
//        // 添加日志验证结果
//        log.info("返回分页结果 - 当前页: {}, 每页大小: {}, 总记录: {}",
//                result.getPageNum(), result.getPageSize(), result.getTotal());
//
//        return Result.success(result);
//    }

    @ApiOperation("分页查询题目")
    @GetMapping("/page")
    public Result<PageResult> page(
            @RequestParam(required = false) String questionType,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer majorId,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageResult result = questionService.getQuestionPage(
                questionType, keyword, majorId, categoryId, pageNum, pageSize);
        return Result.success(result);
    }


    /**
     * 根据ID查看题目详情
     * @param questionId
     * @return
     */
    @GetMapping("/{questionId}")
    @ApiOperation("根据ID查看题目详情")
    public Result<QuestionVO> getQuestionById(@PathVariable Integer questionId) {
        QuestionVO question = questionService.getQuestionById(questionId);
        return Result.success(question);
    }

    /**
     * 添加新题目
     * @param questionDTO
     * @return
     */
    @PostMapping("")
    @ApiOperation("添加新题目")
    @Caching(evict = {@CacheEvict(cacheNames = RedisKeyConstant.REDIS_USER_LISTALL_KEY, allEntries = true)})
    public Result<QuestionVO> addQuestion(@RequestBody QuestionDTO questionDTO) {
        QuestionVO vo = questionService.addQuestion(questionDTO);
        return Result.success(vo);
    }

    /**
     * 编辑更新题目
     * @param questionId
     * @param questionDTO
     * @return
     */
    @PutMapping("/{questionId}")
    @ApiOperation("编辑更新题目")
    public Result<QuestionVO> updateQuestion(
            @PathVariable Integer questionId,
            @RequestBody QuestionDTO questionDTO) {

        QuestionVO vo = questionService.updateQuestion(questionId, questionDTO);
        return Result.success(vo);
    }

    /**
     * 删除题目
     * @param questionId
     * @return
     */
    @DeleteMapping("/{questionId}")
    @ApiOperation("删除题目")
    public Result<String> deleteQuestion(@PathVariable Integer questionId) {
        questionService.deleteQuestion(questionId);
        return Result.success("题目删除成功");
    }
}